// pages/evaluation/evaluation.js
const api = require('../../util')
const app = getApp()
var time = ''
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: app.globalData.statusBarHeight,

        options: {},
        arr: [],
        index: 0,

        active: 0, // 当前展示的题目
        list: [],
        xz: [], // 选中的题目数组
        time: ''
    },
    // 获取测评类别
    getSubjectCategory() {
        var that = this
        api.http('/Subject/getSubjectCategory', 'POST', false, {
        }, function (res) {
            if(res.code == 200) {
                for (let i = 0; i < res.data.length; i++) {
                    if (res.data[i].id == that.data.options.category_id) {
                        that.setData({
                            arr: res.data,
                            index: i
                        })
                    }
                }
                if (typeof(that.data.options.category_id)=='undefined') {
                    that.setData({
                        arr: res.data,
                        index: 0,
                        'options.category_id': res.data[0].id
                    })
                }
                // 获取答卷
                that.getSubject()
            }
        })
    },
    //  答卷
    getSubject() {
        var that = this
        api.http('/Subject/getSubject', 'POST', true, {
            category_id: this.data.options.category_id,
        }, function (res) {
            if(res.code == 200) {
                var xz=[]
                for (let j = 0; j < res.data.length; j++) {
                    var str = ''
                    xz.push(str)
                }
                that.setData({
                    list: res.data,
                    xz
                })
            }
        })
    },
    before() {
        wx.navigateBack({
            delta: 1
        })
    },
    // 下拉触发
    arrChange(e) {
        var that = this
        this.setData({
            index: e.detail.value
        }, () => {
            var id = that.data.arr[that.data.index].id
            that.setData({
                'options.category_id': id,
                active: 0,
                list: []
            }, () => {
                that.getSubject()
            })
        })
    },
    // 点击选择的题目
    optionsactive(e) {
        if (time!=='') {
            return
        }
        time = 1
        var activexz = 'xz['+this.data.active+']'
        var that = this
        this.setData({
            [activexz]: e.currentTarget.dataset.id
        }, () => {
            var active = that.data.active
            if (that.data.active<that.data.xz.length-1) {
                active = that.data.active+1
            }
            time = setTimeout(() => {
                that.setData({
                    active
                }, () => {
                    // 等待隐藏dom隐藏后再清除 *解决贼快点击e.currentTarget.dataset.id为上次dom携带的参数问题
                    setTimeout(() => {
                        clearTimeout(time)
                        time = ''
                    }, 250);
                })
            }, 200);
        })
    },
    // 上一题
    jian() {
        if (this.data.active>=1) {
            this.data.active = this.data.active-1
        }
        var that = this
        this.setData({
            active: that.data.active
        })
    },
    // 提交
    toresult() {
        var that = this
        var arr = []
        for (let i = 0; i < this.data.list.length; i++) {
            var obj={ id: '',questionnaire_id: '',option_id: ''}
            obj.id = this.data.list[i].id
            obj.questionnaire_id = this.data.list[i].questionnaire_id
            obj.option_id = this.data.xz[i]
            arr.push(obj)
        }
        wx.redirectTo({
            url: '/pages/result/result?data='+JSON.stringify(arr)+'&category_id='+this.data.options.category_id+'&title='+this.data.arr[this.data.index].title,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            options
        })
        // 获取测评类别
        this.getSubjectCategory()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage() {

    // }
})